﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VRacer.Packets
{
    public class ScoresPacket : PacketBase
    {
        private Highscores _scores = new Highscores();

        public ScoresPacket()
            : base(PacketType.Scores)
        {

        }

        public ScoresPacket(Highscores scores)
            : this()
        {
            _scores = scores;
        }

        public Highscores Scores
        {
            get { return _scores; }
            set { _scores = value; }
        }

        public override void Read(System.IO.BinaryReader reader)
        {
            base.Read(reader);

            _scores.Clear();
            Score score;

            int i = reader.ReadInt32();
            while (i-- > 0)
            {
                score = new Score();
                score.Player = reader.ReadString();
                score.Result = reader.ReadInt32();
            }
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            base.Write(writer);

            writer.Write(_scores.Count);

            foreach (var score in _scores)
            {
                writer.Write(score.Player);
                writer.Write(score.Result);
            }
        }
    }
}
